
var paginator = function (id, action, total, pageSize, pageIndex) {
    this.first = null;
    this.prev = null;
    this.next = null;
    this.last = null;
    this.pageIndex = 'pageIndex';

    this.init = function (config) {
        if (config !== null) {
            this.first = config["first"];
            this.prev = config["prev"];
            this.next = config["next"];
            this.last = config["last"];
            this.pageIndex = config["pageIndex"];
        }
        return this;
    };

    this.paging = function () {
        if (total <= 0) {
            return false;
        }
        var pageNum = Math.ceil(total / pageSize); //总页数
        var breakPage = 4; // 为第一页时显示的数量
        var currentPosition = 2; //当前页 前面和后面显示页码的数量
        var breakSpace = 2; //最前面的页数量, 和最后面的页数量
        var maxSpace = 4;
        var prevNum = pageIndex - currentPosition; //上一页
        var nextNum = pageIndex + currentPosition; //下一页
        if (prevNum < 1) prevNum = 1; //如果上一页的值小于1, 那么就等于1
        if (nextNum > pageNum) nextNum = pageNum; //如果下一页的值大于总页数, 那么就等于最后一页
        var pageHtml = "";

        if(this.first !== null && this.first !== '') {
            if(pageIndex === 1) {
                pageHtml = '<span class="first">{0}</span>'.format(this.first);
            } else {
                pageHtml = '<a class="first" href="{0}{1}" data-pageIndex="{1}">{2}</a>'.format(action, 1, this.first);
            }
        }
        if (this.prev !== null && this.prev !== '') {
            if(pageIndex === 1) {
                pageHtml += '<span class="prev">{0}</span>'.format(this.prev);
            } else {
                pageHtml += '<a class="prev" href="{0}{1}" data-pageIndex="{1}">{2}</a>'.format(action, pageIndex - 1, this.prev);
            }
        }
        if (prevNum - breakSpace > maxSpace) {
            for (var i = 1; i <= breakSpace; i++) {
                pageHtml += '<a class="num" href="{0}{1}" data-pageIndex="{1}">{1}</a>'.format(action, i);
            }
            pageHtml += '<span class="ellipsis">...</span>';
            for (var i = pageNum - breakPage + 1; i < prevNum; i++) {
                pageHtml += '<a class="num" href="{0}{1}" data-pageIndex="{1}">{1}</a>'.format(action, i);
            }
        } else {
            for (var i = 1; i < prevNum; i++) {
                pageHtml += '<a class="num" href="{0}{1}" data-pageIndex="{1}">{1}</a>'.format(action, i);
            }
        }
        for (var i = prevNum; i <= nextNum; i++) {
            if(pageIndex === i) {
                pageHtml += '<span class="current">{0}</span>'.format(i);
            } else {
                pageHtml += '<a class="num" href="{0}/{1}" data-pageIndex="{1}">{1}</a>'.format(action, i);
            }
        }
        if (pageNum - breakSpace - nextNum + 1 > maxSpace) {
            for (var i = nextNum + 1; i <= breakPage; i++) {
                pageHtml += '<a class="num" href="{0}{1}" data-pageIndex="{1}">{1}</a>'.format(action, i);
            }
            pageHtml += '<span class="ellipsis">...</span>';
            for (var i = pageNum - breakSpace + 1; i <= pageNum; i++) {
                pageHtml += '<a class="num" href="{0}{1}" data-pageIndex="{1}">{1}</a>'.format(action, i);
            }
        } else {
            for (var i = nextNum + 1; i <= pageNum; i++) {
                pageHtml += '<a class="num" href="{0}{1}" data-pageIndex="{1}">{1}</a>'.format(action, i);
            }
        }
        if(this.next !== null && this.next !== '') {
            if(pageIndex === pageNum) {
                pageHtml += '<span class="next">{0}</span>'.format(this.next);
            } else {
                pageHtml += '<a class="next" href="{0}{1}" data-pageIndex="{1}">{2}</a>'.format(action, pageIndex + 1, this.next);
            }
        }
        if(this.last !== null && this.last !== '') {
            if(pageIndex === pageNum) {
                pageHtml += '<span class="last">{0}</span>'.format(this.last);
            } else {
                pageHtml += '<a class="last" href="{0}{1}" data-pageIndex="{1}">{2}</a>'.format(action, pageNum, this.last);
            }
        }
        pageHtml += '<span>当前第{0}页/总计{1}页/每页{2}条/总计{3}条</span>'.format(pageIndex, pageNum, pageSize, total);
        document.getElementById(id).innerHTML = pageHtml;
    }
}
